package com.tomorrow.analyze;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.R.integer;
import android.content.res.Resources;
import android.util.Xml;

import com.tomorrow.weather.application.WeatherApplication;
import com.tomorrow.weather.entity.City;
import com.tomorrow.weather.mInterface.IHotCityData;
import com.tomorrow.weather.ui.R;

public class HotCityDataPull implements IHotCityData
{
	private ArrayList<City> hotCities;
	private City city;
	private InputStream is;
	@Override
	public List<City> getHotCity()
	{
		Resources resources = (WeatherApplication.getContext())
				.getResources();
		is = resources.openRawResource(R.raw.hot_city);
		XmlPullParser parser = Xml.newPullParser();
		try
		{
			parser.setInput(is,"UTF-8");
			int event = parser.getEventType();
			while (event != XmlPullParser.END_DOCUMENT)
			{
				switch (event)
				{
				case XmlPullParser.START_DOCUMENT:
					hotCities = new ArrayList<City>();
					break;
				case XmlPullParser.START_TAG:
					if ("string".equals(parser.getName()))
					{
						city = new City();
						city.setCityId(parser.getAttributeValue(0));
						city.setCityName(parser.nextText());
					}
					break;
				case XmlPullParser.END_TAG:
					if ("string".equals(parser.getName()))
					{
						hotCities.add(city);
						city = null;
					}
				default:
					break;
				}
				event = parser.next();
			}
		}
		catch (XmlPullParserException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return hotCities;
	}

}
